import pygame
import inspect
from typing import NoReturn

from game.event.eventManager import event_decter
from game.container import CONSTANT, Variables, load_barrier
from game.ui.menus import Menus
from game.sprites.level import Level
from game.world.world import World
from game.sound import ChannelManager
from game.ui.component import ComponentsContainer
from game.event.eventManager import Eventbus, EventId
from game.data.assets.assetManager import load_image
    
def init() -> None:

    Variables.screen.fill('black')
    
    Variables.clock = pygame.time.Clock()

    Menus.init()
    ComponentsContainer.init()
    
def run_forever() -> NoReturn:
    init()
    
    # initialize as main_menu
    Variables.current_ui = Menus.main_menu  # type: ignore
    
    world = World()
    level = Level()
    
    ticks = 0
    
    ChannelManager.set_volume_all(0) # Only for test here
    
    pygame.display.set_caption("Vindicare Demo")
    pygame.display.set_icon(load_image("demo/logo.png"))
    
    pygame.display.update()
            
    Variables.clock.tick(CONSTANT.APP.FPS)
    if not Variables.is_game_paused:
        level.update()
                
        level.animate()
                
        if Variables.current_ui:
            Variables.current_ui.update()  # make current ui update and draw
        event_decter()  # detect events
    
    Eventbus.triggerEvent(EventId.game_start)
    Eventbus.triggerEvent(EventId.toggle_game_pause)
    Menus.game_menu.component_list[0].undraw()
    
    load_barrier()
    
    while True:
        
        try:
            pygame.display.update()
            
            Variables.clock.tick(CONSTANT.APP.FPS)
            ticks = (ticks + 1) % CONSTANT.APP.FPS
            _animate = CONSTANT.APP.FPS // 4
            _detect_event = CONSTANT.APP.FPS // 2
            
            if not Variables.is_game_paused:
                level.update()
                
                if ticks % _animate == 0:
                    level.animate()
                
            if Variables.current_ui:
                Variables.current_ui.update()  # make current ui update and draw
            # print(Variables.current_ui, Variables.is_game_paused)
            event_decter()  # detect events
            # print(Variables.is_game_paused)
                        
        except Exception as e:
            raise e























# class You:
#     def __init__(self):
#         self.hobby = ['游戏']
#
#     def join(self, asscociation):
#         pass
#     def express(self, thoughts):
#         pass
#
#     def want(self):
#         pass
#
#     def
# class We:
#     def invite(self, person):
#         pass
#     def listen_to(self, person):
#         pass
#
#
# 编程社 = []
# you = You()
# we = We()
# if '游戏' in you.hobby:
#     you.join('编程社')
#     if you is '希望学到新东西' and '期待做出自己的游戏':
#         we.invite(you.join('编程社'))
# if you in 编程社:
#     while 1+1 == 2:
#         you.express('你的创意和想法')
#         we.listen_to(you)
#     # 甚至没报错